package com.turing.post.cardmanagement.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.cardmanagement.util.CardClassicBindMerchant;
import com.turing.post.cardmanagement.util.CardClassicBindMerchantDao;
import com.turing.post.organization.util.Merchant;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "cardmanagement.impl.CardClassicBindMerchantDaoImpl")
@Remote
@Local
public class CardClassicBindMerchantDaoImpl implements
		CardClassicBindMerchantDao {
	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	/**
	 * 根据卡种类ID检索绑定的所有商户
	 * 
	 * @param cardClassid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<CardClassicBindMerchant> getAllBindMerchantByCardClassicId(
			BigInteger cardClassid) {
		Query query = _em
				.createNativeQuery(
						"select t.* from CardClassicBindMerchant t where t.CardClassicId = ?1",
						CardClassicBindMerchant.class);
		query.setParameter(1, cardClassid);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Merchant> getMerchantListByCardClassicId(PCPSWebInput webInput,
			BigInteger cardClassid, Integer page) {
		String sql = "";
		if (cardClassid == null) {
			return null;
		}
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			sql = "select * from Merchant t where t.merchantno in (select b.merchantno from CardClassic a,"
					+ "CardClassicBindMerchant b  where a.cardclassicid=b.cardclassicid and IssueCompanyNo='"
					+ issueCompanyNo
					+ "' and a.cardclassicid="
					+ cardClassid
					+ ") order by merchantNo";
		} else {
			sql = "select * from Merchant t where t.merchantno in (select b.merchantno from CardClassic a,"
					+ "CardClassicBindMerchant b  where a.cardclassicid=b.cardclassicid and a.cardclassicid="
					+ cardClassid + ") order by merchantNo";
		}
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em.createNativeQuery(sql, Merchant.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		return query.getResultList();
	}

	@Override
	public BigInteger getMerchantTotalByCardClassicId(PCPSWebInput webInput,
			BigInteger cardClassid) {
		String sql = "";
		if (cardClassid == null) {
			return BigInteger.ZERO;
		}
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			sql = "select nvl(count(*),0) from Merchant t where t.merchantno in (select b.merchantno from CardClassic a,"
					+ "CardClassicBindMerchant b  where a.cardclassicid=b.cardclassicid and IssueCompanyNo='"
					+ issueCompanyNo
					+ "' and a.cardclassicid="
					+ cardClassid
					+ ")";
		} else {
			sql = "select nvl(count(*),0) from Merchant t where t.merchantno in (select b.merchantno from CardClassic a,"
					+ "CardClassicBindMerchant b  where a.cardclassicid=b.cardclassicid and a.cardclassicid="
					+ cardClassid + ")";
		}
		Query query = _em.createNativeQuery(sql);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void deleteCardClassicBindMerchant(String merchantNo)
			throws PCPSApplicationException {
		String sql = "delete from CardClassicBindMerchant where merchantNo =?1";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, merchantNo);
		query.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CardClassicBindMerchant> getCardClassicBindMerchatListByMerchantNo(
			String merchantNo) {
		String sql = "select * from CardClassicBindMerchant where merchantno =?1 order by cardClassicId";
		Query query = _em.createNativeQuery(sql, CardClassicBindMerchant.class);
		query.setParameter(1, merchantNo);
		return query.getResultList();
	}

	@Override
	public void insertCardClassicBindMerchant(String merchantNo,
			BigInteger[] arrInt) throws PCPSApplicationException {
		// 删除已绑定数据
		deleteCardClassicBindMerchant(merchantNo);
		CardClassicBindMerchant ccbm = null;
		for (BigInteger b : arrInt) {
			ccbm = new CardClassicBindMerchant();
			ccbm.setMerchantNo(merchantNo);
			ccbm.setCardClassicId(b);
			insertCardClassicBindMerchant(ccbm);
		}
	}

	@Override
	public void insertCardClassicBindMerchant(CardClassicBindMerchant ccbm)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, ccbm);
	}
}
